using System;
using System.Collections.Generic;
using System.Text;
using Lizk.SimpleHUD;

namespace SharpMedia.Controls
{
    public class HUDTextBox : HUDContainerSimple
    {
        private string blankImageFile, font;
        // Debug image
        private HUDImage marker;

        private HUDLabel label;
        public HUDLabel Label
        {
            get { return label; }
        }

        public HUDTextBox(string blankImageFile, string font)
        {
            this.blankImageFile = blankImageFile;
            this.font = font;

            HUDImage backgroundImage = new HUDImage(blankImageFile);
            backgroundImage.Color = ColorS.White;
            backgroundImage.Bounds = new RectangleS(1, 1, Bounds.Width -2 , Bounds.Height - 2);
            backgroundImage.Anchor = Anchor.All;
            backgroundImage.ConstrainProportions = false;
            Add(backgroundImage);

            HUDImage topLine = new HUDImage(blankImageFile);
            topLine.Color = ColorS.Black;
            topLine.Bounds = new RectangleS(0, 0, Bounds.Width, 1);
            topLine.Anchor = Anchor.All ^ Anchor.Bottom;
            topLine.ConstrainProportions = false;
            Add(topLine);

            HUDImage leftLine = new HUDImage(blankImageFile);
            leftLine.Color = ColorS.Black;
            leftLine.Bounds = new RectangleS(0, 1, 1, Bounds.Height - 2);
            leftLine.Anchor = Anchor.All ^ Anchor.Right;
            leftLine.ConstrainProportions = false;
            Add(leftLine);

            HUDImage rightLine = new HUDImage(blankImageFile);
            rightLine.Color = ColorS.Black;
            rightLine.Bounds = new RectangleS(Bounds.Width - 1, 1, 1, Bounds.Height -2 );
            rightLine.Anchor = Anchor.All ^ Anchor.Left;
            rightLine.ConstrainProportions = false;
            Add(rightLine);

            HUDImage bottomLine = new HUDImage(blankImageFile);
            bottomLine.Color = ColorS.Black;
            bottomLine.Bounds = new RectangleS(0, Bounds.Height-1, Bounds.Width, 1);
            bottomLine.Anchor = Anchor.All ^ Anchor.Top;
            bottomLine.ConstrainProportions = false;
            Add(bottomLine);
            
            label = new HUDLabel("", font);
            label.Color = ColorS.Black;
            label.WordBreak = false;
            label.CharacterOffset = new PointS(-2, -5);
            label.Bounds = new RectangleS(1, 1, Bounds.Width - 2, Bounds.Height - 2);
            label.Anchor = Anchor.All;
            Add(label);

            marker = new HUDImage("white.png");
            marker.Color = ColorS.Red;
            marker.ConstrainProportions = false;
            marker.Alpha = 200;
            //Add(marker);

        }

        public float GetTextWidth()
        {

            bool dirty = label.Dirty;
            int countWhiteSpace = 0;
            string s = label.Text;
            while (s.EndsWith(" "))
            {
                s = s.Remove(s.Length - 1);
                countWhiteSpace++;
            }

            List<Lizk.SimpleHUD.HUDFont.DrawInfo> infos = label.GetCharInfo();
            HUDFont hudFont = HUDFont.GetFont(font);
            label.Dirty = dirty;
            if(infos.Count > 0) {
                label.Size = new SizeS(Bounds.Width + infos[infos.Count - 1].Destination.Right + countWhiteSpace * 10, label.Bounds.Height);
                label.Destination = new PointS(infos[infos.Count - 1].Destination.Right + countWhiteSpace * 10 - Bounds.Width > 0 ? Bounds.Width - infos[infos.Count - 1].Destination.Right - 20 - countWhiteSpace * 10 : 0, label.Location.Y);
                marker.Bounds = infos[infos.Count - 1].Destination;
                marker.Location += label.Destination + new PointS(hudFont.PadLeft + countWhiteSpace*10, hudFont.PadUp);
                marker.Size -= new SizeS(hudFont.PadLeft + hudFont.PadRight, hudFont.PadUp + hudFont.PadDown);
                return marker.Bounds.Right;
            } else 
                return countWhiteSpace * 10f;

        }

    }
}
